package com.liangsui.mss.utils;

import com.liangsui.mss.constants.ResultCode;
import com.liangsui.mss.exception.AlertException;
import com.liangsui.mss.mq.dto.BaseMqDto;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * ValidationUtils
 *
 * @author zhangjiawei
 * @email 1533167396@qq.com
 * @date 2022/12/8 17:41
 */
public class ValidationUtils {

    public static void isTrue(Boolean value, int code, String msg) {
        if (!Boolean.TRUE.equals(value)) {
            throw new AlertException(code, msg);
        }
    }

    public static void isTrue(Boolean value, String msg) {
        if (!Boolean.TRUE.equals(value)) {
            throw new AlertException(ResultCode.ALERT_ERROR.getState(), msg);
        }
    }

    public static void isTrue(Boolean value, ResultCode respCode) {
        if (!Boolean.TRUE.equals(value)) {
            throw new AlertException(respCode.getState(), respCode.getMessage());
        }
    }

    public static void isFalse(Boolean value, int code, String msg) {
        if (!Boolean.TRUE.equals(value)) {
            throw new AlertException(code, msg);
        }
    }

    public static void isFalse(Boolean value, String msg) {
        if (!Boolean.TRUE.equals(value)) {
            throw new AlertException(ResultCode.ALERT_ERROR.getState(), msg);
        }
    }

    public static void isFalse(Boolean value, ResultCode respCode) {
        if (!Boolean.TRUE.equals(value)) {
            throw new AlertException(respCode.getState(), respCode.getMessage());
        }
    }

    public static void notEmpty(String value, int code, String msg) {
        if (value == null || value.isEmpty()) {
            throw new AlertException(code, msg);
        }
    }

    public static void notEmpty(String value, String msg) {
        if (value == null || value.isEmpty()) {
            throw new AlertException(ResultCode.ALERT_ERROR.getState(), msg);
        }
    }

    public static void notEmpty(String value, ResultCode respCode) {
        if (value == null || value.isEmpty()) {
            throw new AlertException(respCode.getState(), respCode.getMessage());
        }
    }

    public static void notNullParam(BaseMqDto data) {
        if (data == null) {
            throw new AlertException();
        }
    }

    public static void notNullParam(String topic) {
        if (topic == null || topic.isEmpty()) {
            throw new AlertException();
        }
    }

    public static void notNullParam(Object data) {
        if (data == null) {
            throw new AlertException();
        }
        if (data instanceof String) {
            notNullParam((String) data);
        }
        if (data instanceof List) {
            isListEmpty(data);
        }
        if(data instanceof Map)
        {
            isMapEmpty(data);
        }
    }

    private static void isMapEmpty(Object source) {
        Map map = (Map) source;
        Set keySet = map.keySet();
        if (keySet.isEmpty()) {
            throw new AlertException();
        }
    }

    public static boolean isListEmpty(Object source) {
        List li = (List) source;
        if (li == null || li.size() == 0) {
            return true;
        } else {
            throw new AlertException();
        }
    }
}
